﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace QLNHK.GenKey
{
	public partial class ThongTinDonVi : ICloneable
	{
		#region Properties.
		
		public Guid ID { set; get; }
		public string MaDonVi { set; get; }
		public string TenDonVi { set; get; }
		public DateTime NgayMua { set; get; }
		public int SoLuongBanQuyenMua { set; get; }
		public string TinhTrangThanhToan { set; get; }
		public string GhiChu { set; get; }
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Methods
		protected static List<ThongTinDonVi> ConvertToCollection(IDataReader reader)
		{
			List<ThongTinDonVi> collection = new List<ThongTinDonVi>();
			while (reader.Read())
			{
				ThongTinDonVi entity = new ThongTinDonVi();
				if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetGuid(reader.GetOrdinal("ID"));
				if (!reader.IsDBNull(reader.GetOrdinal("MaDonVi"))) entity.MaDonVi = reader.GetString(reader.GetOrdinal("MaDonVi"));
				if (!reader.IsDBNull(reader.GetOrdinal("TenDonVi"))) entity.TenDonVi = reader.GetString(reader.GetOrdinal("TenDonVi"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgayMua"))) entity.NgayMua = reader.GetDateTime(reader.GetOrdinal("NgayMua"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoLuongBanQuyenMua"))) entity.SoLuongBanQuyenMua = reader.GetInt32(reader.GetOrdinal("SoLuongBanQuyenMua"));
				if (!reader.IsDBNull(reader.GetOrdinal("TinhTrangThanhToan"))) entity.TinhTrangThanhToan = reader.GetString(reader.GetOrdinal("TinhTrangThanhToan"));
				if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
				collection.Add(entity);
			}
			reader.Close();
			return collection;
		}
		
		public static bool Find(List<ThongTinDonVi> collection, Guid id)
        {
            foreach (ThongTinDonVi item in collection)
            {
                if (item.ID == id)
                {
                    return true;
                }
            }

            return false;
        }
		
		public static void UpdateDataSet(DataSet ds)
        {
            string insert = "Insert INTO t_ThongTinDonVi VALUES(@ID, @MaDonVi, @TenDonVi, @NgayMua, @SoLuongBanQuyenMua, @TinhTrangThanhToan, @GhiChu)";
            string update = "UPDATE t_ThongTinDonVi SET MaDonVi = @MaDonVi, TenDonVi = @TenDonVi, NgayMua = @NgayMua, SoLuongBanQuyenMua = @SoLuongBanQuyenMua, TinhTrangThanhToan = @TinhTrangThanhToan, GhiChu = @GhiChu WHERE ID = @ID";
            string delete = "DELETE FROM t_ThongTinDonVi WHERE ID = @ID";

			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();

            System.Data.Common.DbCommand InsertCommand = db.GetSqlStringCommand(insert);
			db.AddInParameter(InsertCommand, "@ID", SqlDbType.UniqueIdentifier, "ID", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@MaDonVi", SqlDbType.NVarChar, "MaDonVi", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@TenDonVi", SqlDbType.NVarChar, "TenDonVi", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@NgayMua", SqlDbType.DateTime, "NgayMua", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@SoLuongBanQuyenMua", SqlDbType.Int, "SoLuongBanQuyenMua", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@TinhTrangThanhToan", SqlDbType.NVarChar, "TinhTrangThanhToan", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@GhiChu", SqlDbType.NVarChar, "GhiChu", DataRowVersion.Current);

            System.Data.Common.DbCommand UpdateCommand = db.GetSqlStringCommand(update);
			db.AddInParameter(UpdateCommand, "@ID", SqlDbType.UniqueIdentifier, "ID", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@MaDonVi", SqlDbType.NVarChar, "MaDonVi", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@TenDonVi", SqlDbType.NVarChar, "TenDonVi", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@NgayMua", SqlDbType.DateTime, "NgayMua", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@SoLuongBanQuyenMua", SqlDbType.Int, "SoLuongBanQuyenMua", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@TinhTrangThanhToan", SqlDbType.NVarChar, "TinhTrangThanhToan", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@GhiChu", SqlDbType.NVarChar, "GhiChu", DataRowVersion.Current);
			
            System.Data.Common.DbCommand DeleteCommand = db.GetSqlStringCommand(delete);
			db.AddInParameter(DeleteCommand, "@ID", SqlDbType.UniqueIdentifier, "ID", DataRowVersion.Current);

            db.UpdateDataSet(ds, ds.Tables[0].TableName, InsertCommand, UpdateCommand, DeleteCommand, UpdateBehavior.Standard);
        }
				
		public static void UpdateDataSet(DataSet ds, string tableName)
        {
            string insert = "Insert INTO t_ThongTinDonVi VALUES(@ID, @MaDonVi, @TenDonVi, @NgayMua, @SoLuongBanQuyenMua, @TinhTrangThanhToan, @GhiChu)";
            string update = "UPDATE t_ThongTinDonVi SET MaDonVi = @MaDonVi, TenDonVi = @TenDonVi, NgayMua = @NgayMua, SoLuongBanQuyenMua = @SoLuongBanQuyenMua, TinhTrangThanhToan = @TinhTrangThanhToan, GhiChu = @GhiChu WHERE ID = @ID";
            string delete = "DELETE FROM t_ThongTinDonVi WHERE ID = @ID";

			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();

            System.Data.Common.DbCommand InsertCommand = db.GetSqlStringCommand(insert);
			db.AddInParameter(InsertCommand, "@ID", SqlDbType.UniqueIdentifier, "ID", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@MaDonVi", SqlDbType.NVarChar, "MaDonVi", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@TenDonVi", SqlDbType.NVarChar, "TenDonVi", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@NgayMua", SqlDbType.DateTime, "NgayMua", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@SoLuongBanQuyenMua", SqlDbType.Int, "SoLuongBanQuyenMua", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@TinhTrangThanhToan", SqlDbType.NVarChar, "TinhTrangThanhToan", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@GhiChu", SqlDbType.NVarChar, "GhiChu", DataRowVersion.Current);

            System.Data.Common.DbCommand UpdateCommand = db.GetSqlStringCommand(update);
			db.AddInParameter(UpdateCommand, "@ID", SqlDbType.UniqueIdentifier, "ID", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@MaDonVi", SqlDbType.NVarChar, "MaDonVi", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@TenDonVi", SqlDbType.NVarChar, "TenDonVi", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@NgayMua", SqlDbType.DateTime, "NgayMua", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@SoLuongBanQuyenMua", SqlDbType.Int, "SoLuongBanQuyenMua", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@TinhTrangThanhToan", SqlDbType.NVarChar, "TinhTrangThanhToan", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@GhiChu", SqlDbType.NVarChar, "GhiChu", DataRowVersion.Current);
			
            System.Data.Common.DbCommand DeleteCommand = db.GetSqlStringCommand(delete);
			db.AddInParameter(DeleteCommand, "@ID", SqlDbType.UniqueIdentifier, "ID", DataRowVersion.Current);

            db.UpdateDataSet(ds, tableName, InsertCommand, UpdateCommand, DeleteCommand, UpdateBehavior.Standard);
        }

		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Select methods.
		
		public static ThongTinDonVi Load(Guid id)
		{
			const string spName = "[dbo].[p_ThongTinDonVi_Load]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ID", SqlDbType.UniqueIdentifier, id);
            IDataReader reader = db.ExecuteReader(dbCommand);
			List<ThongTinDonVi> collection = ConvertToCollection(reader);	
			if (collection.Count > 0)
			{
				return collection[0];
			}
			return null;
		}		
		
		//---------------------------------------------------------------------------------------------
		

		public static DataSet SelectAll()
        {
            const string spName = "[dbo].[p_ThongTinDonVi_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			
            return db.ExecuteDataSet(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_ThongTinDonVi_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteDataSet(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
				
		public static IDataReader SelectReaderAll()
        {
            const string spName = "[dbo].[p_ThongTinDonVi_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
            return db.ExecuteReader(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_ThongTinDonVi_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteReader(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert methods.
		
		public static int InsertThongTinDonVi(Guid iD, string maDonVi, string tenDonVi, DateTime ngayMua, int soLuongBanQuyenMua, string tinhTrangThanhToan, string ghiChu)
		{
			ThongTinDonVi entity = new ThongTinDonVi();	
            entity.ID = iD;
			entity.MaDonVi = maDonVi;
			entity.TenDonVi = tenDonVi;
			entity.NgayMua = ngayMua;
			entity.SoLuongBanQuyenMua = soLuongBanQuyenMua;
			entity.TinhTrangThanhToan = tinhTrangThanhToan;
			entity.GhiChu = ghiChu;
			return entity.Insert();
		}
		
		public int Insert()
		{
			return this.Insert(null);
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public int Insert(SqlTransaction transaction)
		{			
			const string spName = "[dbo].[p_ThongTinDonVi_Insert]";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ID", SqlDbType.UniqueIdentifier, ID);
			db.AddInParameter(dbCommand, "@MaDonVi", SqlDbType.NVarChar, MaDonVi);
			db.AddInParameter(dbCommand, "@TenDonVi", SqlDbType.NVarChar, TenDonVi);
			db.AddInParameter(dbCommand, "@NgayMua", SqlDbType.DateTime, NgayMua.Year <= 1753 ? DBNull.Value : (object) NgayMua);
			db.AddInParameter(dbCommand, "@SoLuongBanQuyenMua", SqlDbType.Int, SoLuongBanQuyenMua);
			db.AddInParameter(dbCommand, "@TinhTrangThanhToan", SqlDbType.NVarChar, TinhTrangThanhToan);
			db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);
			
			if (transaction != null)
			{
				return db.ExecuteNonQuery(dbCommand, transaction);
			}
            else
			{
				return db.ExecuteNonQuery(dbCommand);
			}			
		}
				
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert / Update methods.
		
		public static int InsertUpdateThongTinDonVi(Guid id, string maDonVi, string tenDonVi, DateTime ngayMua, int soLuongBanQuyenMua, string tinhTrangThanhToan, string ghiChu)
		{
			ThongTinDonVi entity = new ThongTinDonVi();			
			entity.ID = id;
			entity.MaDonVi = maDonVi;
			entity.TenDonVi = tenDonVi;
			entity.NgayMua = ngayMua;
			entity.SoLuongBanQuyenMua = soLuongBanQuyenMua;
			entity.TinhTrangThanhToan = tinhTrangThanhToan;
			entity.GhiChu = ghiChu;
			return entity.InsertUpdate();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int InsertUpdate()
		{
			return this.InsertUpdate(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int InsertUpdate(SqlTransaction transaction)
		{			
			const string spName = "p_ThongTinDonVi_InsertUpdate";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ID", SqlDbType.UniqueIdentifier, ID);
			db.AddInParameter(dbCommand, "@MaDonVi", SqlDbType.NVarChar, MaDonVi);
			db.AddInParameter(dbCommand, "@TenDonVi", SqlDbType.NVarChar, TenDonVi);
			db.AddInParameter(dbCommand, "@NgayMua", SqlDbType.DateTime, NgayMua.Year <= 1753 ? DBNull.Value : (object) NgayMua);
			db.AddInParameter(dbCommand, "@SoLuongBanQuyenMua", SqlDbType.Int, SoLuongBanQuyenMua);
			db.AddInParameter(dbCommand, "@TinhTrangThanhToan", SqlDbType.NVarChar, TinhTrangThanhToan);
			db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);			
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Update methods.
		
		public static int UpdateThongTinDonVi(Guid id, string maDonVi, string tenDonVi, DateTime ngayMua, int soLuongBanQuyenMua, string tinhTrangThanhToan, string ghiChu)
		{
			ThongTinDonVi entity = new ThongTinDonVi();			
			entity.ID = id;
			entity.MaDonVi = maDonVi;
			entity.TenDonVi = tenDonVi;
			entity.NgayMua = ngayMua;
			entity.SoLuongBanQuyenMua = soLuongBanQuyenMua;
			entity.TinhTrangThanhToan = tinhTrangThanhToan;
			entity.GhiChu = ghiChu;
			return entity.Update();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int Update()
		{
			return this.Update(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Update(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_ThongTinDonVi_Update]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ID", SqlDbType.UniqueIdentifier, ID);
			db.AddInParameter(dbCommand, "@MaDonVi", SqlDbType.NVarChar, MaDonVi);
			db.AddInParameter(dbCommand, "@TenDonVi", SqlDbType.NVarChar, TenDonVi);
			db.AddInParameter(dbCommand, "@NgayMua", SqlDbType.DateTime, NgayMua.Year <= 1753 ? DBNull.Value : (object) NgayMua);
			db.AddInParameter(dbCommand, "@SoLuongBanQuyenMua", SqlDbType.Int, SoLuongBanQuyenMua);
			db.AddInParameter(dbCommand, "@TinhTrangThanhToan", SqlDbType.NVarChar, TinhTrangThanhToan);
			db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
				
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Delete methods.
		
		public static int DeleteThongTinDonVi(Guid id)
		{
			ThongTinDonVi entity = new ThongTinDonVi();
			entity.ID = id;
			
			return entity.Delete();
		}
		
		public int Delete()
		{
			return this.Delete(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Delete(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_ThongTinDonVi_Delete]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ID", SqlDbType.UniqueIdentifier, ID);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		
		
		public static int DeleteDynamic(string whereCondition)
		{
			const string spName = "[dbo].[p_ThongTinDonVi_DeleteDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            
            return db.ExecuteNonQuery(dbCommand);   
		}
		//---------------------------------------------------------------------------------------------
		
		#endregion
		
		
        #region ICloneable Members

        public object Clone()
        {
            return base.MemberwiseClone();
        }

        #endregion
	}	
}